import { useState } from 'react';

type ToggleTypeProp = boolean | number | string | undefined;

export const useToggle = (initStatus: ToggleTypeProp = false,reverseStatus: ToggleTypeProp = true) => {
    /** 开关状态 */
    const [state, setState] = useState<ToggleTypeProp>(initStatus);
    /** 设置状态，默认与当前状态相反 */
    const toggle = (status:ToggleTypeProp = undefined)=>{
        if(status) setState(status);
        else{
            setState(state===initStatus?reverseStatus:initStatus);
        }
    }
    /** 设置初始状态 */
    const setInitStatus = ()=>setState(initStatus);
    /** 设置反转状态 */
    const setReverseStatus = ()=>setState(reverseStatus);
    return [state,{toggle,setInitStatus,setReverseStatus}]
}